package com.ty.springbootredis.unit;

import redis.clients.jedis.Jedis;

/**
 * RedisUtil
 *
 * @aurhor Administrator  whs
 * @since 2024/9/25
 */
public class RedisUtil {

    /**
     * 设置 key 的有效期
     * @param key
     * @param exTime
     * @return
     */
    public static Long expire(String key, int exTime) {
        Jedis jedis = null;
        Long result = null;
        try {
            // 从Redis 连接池获得 jedis 对象
            jedis = RedisPool.getJedis();
            result = jedis.expire(key, exTime);
        }catch (Exception e){
            e.printStackTrace();
            return result;
        }finally {
            RedisPool.returnJedis(jedis);
        }
        return result;
    }

    public static String setEx(String key,String value, int exTime) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.setex(key, exTime, value);
        }catch (Exception e){
            e.printStackTrace();
            return result;
        }finally {
            RedisPool.returnJedis(jedis);
        }
        return result;
    }

    public static String set(String key,String value) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.set(key, value);
        }catch (Exception e){
            e.printStackTrace();
            return result;
        }finally {
            RedisPool.returnJedis(jedis);
        }
        return result;
    }

    public static  String get(String key) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.get(key);
        }catch (Exception e){
            e.printStackTrace();
            return result;
        }finally {
            RedisPool.returnJedis(jedis);
        }
        return result;
    }

    public static  Long del(String key) {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.del(key);
        }catch (Exception e){
            e.printStackTrace();
            return result;
        }finally {
            RedisPool.returnJedis(jedis);
        }
        return result;
    }

}
